package com.ruoyi.device.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.device.domain.DeviceBorrow;
import com.ruoyi.device.domain.DeviceInfo;
import com.ruoyi.device.mapper.DeviceBorrowMapper;
import com.ruoyi.device.service.IDeviceBorrowService;
import com.ruoyi.device.service.IDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @auth
 * @date
 */
@Service
public class DeviceBorrowServiceImpl implements IDeviceBorrowService {
    @Autowired(required = false)
    private DeviceBorrowMapper borrowMapper;

    @Autowired
    private IDeviceInfoService deviceService;

    /**
     * 查询借用信息
     * @param id
     * @return
     */
    @Override
    public DeviceBorrow getBorrowById(Long id) {
        return borrowMapper.selectBorrowById(id);
    }

    /**
     * 查询借用信息列表
     * @param params
     * @return
     */
    @Override
    public List<DeviceBorrow> getBorrowList(DeviceBorrow params) {
        return borrowMapper.selectBorrowList(params);
    }

    /**
     * 新增借用信息
     * @param borrow
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addBorrow(DeviceBorrow borrow) {
        //把设备信息设置成已借出状态
        DeviceInfo device = deviceService.getDeviceById(borrow.getDeviceId());
        device.setDeviceStatus(1);
        deviceService.editDevice(device);

        //插入借用记录
        return borrowMapper.insertBorrow(borrow);
    }
    /**
     * 修改设备信息
     * @param borrow
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int editBorrow(DeviceBorrow borrow) {
        //把设备归还设置设备信息的借用状态
        DeviceInfo device = deviceService.getDeviceById(borrow.getDeviceId());
        device.setDeviceStatus(borrow.getDeviceStatus());
        deviceService.editDevice(device);

        //修改借用记录
        borrow.setUpdateTime(DateUtils.getNowDate());
        return borrowMapper.updateBorrow(borrow);
    }
    /**
     * 批量删除设备信息
     * @param ids
     * @return
     */
    @Override
    public int removeBorrowByIds(Long[] ids) {
        return borrowMapper.deleteBorrowByIds(ids);
    }

}
